<?php

/**
 * Zend Framework
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://framework.zend.com/license/new-bsd
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@zend.com so we can send you a copy immediately.
 *
 * @category   Zend
 * @package    Zend_Gdata
 * @copyright  Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 */

/**
 * @see Zend_Gdata_Extension
 */
require_once 'Zend/Gdata/Extension.php';

/**
 * @see Zend_Gdata_Finance_Extension_Commission
 */
require_once 'Zend/Gdata/Finance/Extension/Commission.php';

/**
 * @see Zend_Gdata_Finance_Extension_Price
 */
require_once 'Zend/Gdata/Finance/Extension/Price.php';

/**
 * Data model class to represent Transaction Data
 *
 * @category   Zend
 * @package    Zend_Gdata
 * @copyright  Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 */
class Zend_Gdata_Finance_Extension_TransactionData extends Zend_Gdata_Extension
{
	protected $_rootNamespace = 'gf';
	protected $_rootElement = 'transactionData';
	/**
	 * @var String
	 */
	protected $_type = null;
	/**
	 * @var String
	 */
	protected $_date = null;
	/**
	 * @var String
	 */
	protected $_shares = null;
	/**
	 * @var String
	 */
	protected $_notes = null;
	/**
	 * @var Zend_Gdata_Finance_Extension_Commission
	 */
	protected $_commission = null;
	/**
	 * @var Zend_Gdata_Finance_Extension_Price
	 */
	protected $_price = null;

	/**
	 * Constructs a new Zend_Gdata_Finance_Extension_TransactionData object.
	 *
	 * @param string $type A Buy or Sell order
	 */
	public function __construct($type = null)
	{
		foreach (Zend_Gdata_Finance::$namespaces as $nsPrefix => $nsUri) {
			$this->registerNamespace($nsPrefix, $nsUri);
		}
		$this->_price = new Zend_Gdata_Finance_Extension_Price('0','USD');
		$this->_commission = new Zend_Gdata_Finance_Extension_Commission('0','USD');
		parent::__construct();
		$this->_type = $type;
	}

	/**
	 * Retrieves a DOMElement which corresponds to this element and all
	 * child properties.  This is used to build an entry back into a DOM
	 * and eventually XML text for sending to the server upon updates, or
	 * for application storage/persistence.
	 *
	 * @param DOMDocument $doc The DOMDocument used to construct DOMElements
	 * @return DOMElement The DOMElement representing this element and all
	 * child properties.
	 */
	public function getDOM($doc = null)
	{
		$element = parent::getDOM($doc);
		if ($this->_type != null)
		{
			$element->setAttribute('type', $this->_type);
		}
		if ($this->_date != null)
		{
			$element->setAttribute('date', $this->_date);
		}
		if ($this->_shares != null)
		{
			$element->setAttribute('shares', $this->_shares);
		}
		if ($this->_notes != null)
		{
			$element->setAttribute('notes', $this->_notes);
		}
		if ($this->_commission != null)
		{
			$element->appendChild($this->_commission->getDOM($element->ownerDocument));
		}
		if ($this->_price != null)
		{
			$element->appendChild($this->_price->getDOM($element->ownerDocument));
		}

		return $element;
	}

	/**
	 * Given a DOMNode representing an attribute, tries to map the data into
	 * instance members.  If no mapping is defined, the name and value are
	 * stored in an array.
	 *
	 * @param DOMNode $attribute The DOMNode attribute needed to be handled
	 */
	protected function takeAttributeFromDOM($attribute)
	{
		switch ($attribute->localName) {
			case 'type':
				$this->_type = $attribute->nodeValue;
				break;
			case 'date':
				$this->_date = $attribute->nodeValue;
				break;
			case 'shares':
				$this->_shares = $attribute->nodeValue;
				break;
			case 'notes':
				$this->_notes = $attribute->nodeValue;
				break;

			default:
				parent::takeAttributeFromDOM($attribute);
		}
	}


	/**
	 * Creates individual Entry objects of the appropriate type and
	 * stores them as members of this entry based upon DOM data.
	 *
	 * @param DOMNode $child The DOMNode to process
	 */
	protected function takeChildFromDOM($child)
	{
		$absoluteNodeName = $child->namespaceURI . ':' . $child->localName;

		switch ($absoluteNodeName) {
			case $this->lookupNamespace('gf') . ':' . 'commission':
				$obj = new Zend_Gdata_Finance_Extension_Commission();
				$obj->transferFromDOM($child);
				$this->_commission = $obj;
				break;
			case $this->lookupNamespace('gf') . ':' . 'price':
				$obj = new Zend_Gdata_Finance_Extension_Price();
				$obj->transferFromDOM($child);
				$this->_price = $obj;
				break;
			default:
				parent::takeChildFromDOM($child);
				break;
		}
	}


	/**
	 * Get the value for this elements type  attribute.
	 *
	 * @return string $type The requested attribute.
	 */
	public function getType()
	{
		return $this->_type;
	}

	/**
	 * Set the value for this elements type attribute.
	 *
	 * @param string $value The desired value for this attribute.
	 * @return Zend_Gdata_Finance_Extension_PortFolioData The element being modified.
	 */
	public function setType($value)
	{
		if ( $value == "Buy" or $value == "Sell" or $value = "Buy to Cover" or $value = "Sell Short" )
		$this->_type = $value;
		else
		throw new Exception("Type needs to be one of thse values: Buy,Sell, Buy to Cover, Sell Short");
			
		return $this;
	}


	/**
	 * Get the value for this elements date  attribute.
	 *
	 * @return string $date The requested attribute.
	 */
	public function getDate()
	{
		return $this->_date;
	}

	/**
	 * Set the value for this elements date attribute.
	 *
	 * @param string $value The desired value for this attribute.
	 * @return Zend_Gdata_Finance_Extension_PortFolioData The element being modified.
	 */
	public function setdate($value)
	{
		if ( $value != null )
		{
			$this->_date = $value."T00:00:00.000";
		}
		return $this;
	}

	/**
	 * Get the value for this elements shares  attribute.
	 *
	 * @return string $shares The requested attribute.
	 */
	public function getShares()
	{
		return $this->_shares;
	}

	/**
	 * Set the value for this elements shares attribute.
	 *
	 * @param string $value The desired value for this attribute.
	 * @return Zend_Gdata_Finance_Extension_PortFolioData The element being modified.
	 */
	public function setShares($value)
	{
		$this->_shares = $value;
		return $this;
	}

	/**
	 * Get the value for this elements notes  attribute.
	 *
	 * @return string $notes The requested attribute.
	 */
	public function getNotes()
	{
		return $this->_notes;
	}

	/**
	 * Set the value for this elements notes attribute.
	 *
	 * @param string $value  The desired value for this attribute.
	 * @return Zend_Gdata_Finance_Extension_PortFolioData The element being modified.
	 */
	public function setNotes($value)
	{
		$this->_notes = $value;
		return $this;
	}

	/**
	 * Get the value for this elements commission  attribute.
	 *
	 * @return Zend_Gdata_Finance_Extension_Commission $commission The requested attribute.
	 */
	public function getCommission()
	{
		return $this->_commission;
	}

	/**
	 * Set the value for this elements Commission attribute.
	 *
	 * @param Zend_Gdata_Finance_Extension_Commission $value The desired value for this attribute.
	 * @return Zend_Gdata_Finance_Extension_PortFolioData The element being modified.
	 */
	public function setCommission(Zend_Gdata_Finance_Extension_Commission $value)
	{
		$this->_commission = $value;
		return $this;
	}

	/**
	 * Get the value for this elements price  attribute.
	 *
	 * @return Zend_Gdata_Finance_Extension_Price $price The requested attribute.
	 */
	public function getPrice()
	{
		return $this->_price;
	}

	/**
	 * Set the value for this elements Price attribute.
	 *
	 * @param Zend_Gdata_Finance_Extension_Price $value  The desired value for this attribute.
	 * @return Zend_Gdata_Finance_Extension_PortFolioData The element being modified.
	 */
	public function setPrice($value)
	{
		$this->_price = $value;
		return $this;
	}
}